package com.jic.order.controller;
import com.jic.common.base.vo.RestResult;
import com.jic.order.constant.consist.ResponseCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.order.model.vo.request.MAfterSalesReasonsRequest;
import com.jic.order.model.vo.response.MAfterSalesReasonsResponse;
import com.jic.order.model.entity.MAfterSalesReasons;
import com.jic.order.service.MAfterSalesReasonsService;
/**
 * @Author: 上海奇陌科技有限公司 
 * @date: 2020-11-25 16:09:40
 */
 @RestController
 @Slf4j
 @RequestMapping("/mAfterSalesReasons")
 @Api(tags = " [售后原因表]控制类-PC")
public class MAfterSalesReasonsController {
	@Autowired
	private MAfterSalesReasonsService service;
	
	
     /**
      *新增记录
      * @param request 新增[售后原因表]请求参数
      * @return MAfterSalesReasonsResponse 新增[售后原因表]响应参数
      * author 上海奇陌科技有限公司
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[售后原因表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MAfterSalesReasonsRequest request) throws Throwable {
     		MAfterSalesReasons mAfterSalesReasons = new MAfterSalesReasons();
    		BeanUtils.copyProperties(request, mAfterSalesReasons);
    	    long flag = service.save(mAfterSalesReasons);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[售后原因表]请求参数
      * @return MAfterSalesReasonsResponse 新增或修改[售后原因表]响应参数
      * author 上海奇陌科技有限公司
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[售后原因表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody MAfterSalesReasonsRequest request) throws Throwable {
      		MAfterSalesReasons mAfterSalesReasons = new MAfterSalesReasons();
		     BeanUtils.copyProperties(request, mAfterSalesReasons);
	         long flag =   service.saveOrUpdate(mAfterSalesReasons);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[售后原因表]请求参数
      * @return MAfterSalesReasonsResponse 删除[售后原因表]响应参数
      * author 上海奇陌科技有限公司
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[售后原因表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody MAfterSalesReasonsRequest request) throws Throwable {
      		MAfterSalesReasons mAfterSalesReasons = new MAfterSalesReasons();
	     	BeanUtils.copyProperties(request, mAfterSalesReasons);
	         long flag = service.deleteByPrimaryKeyLogically(mAfterSalesReasons);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[售后原因表]请求参数
      * @return MAfterSalesReasonsResponse 修改[售后原因表]响应参数
      * author 上海奇陌科技有限公司
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[售后原因表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody MAfterSalesReasonsRequest request) throws Throwable {
      		MAfterSalesReasons mAfterSalesReasons = new MAfterSalesReasons();
	     	BeanUtils.copyProperties(request, mAfterSalesReasons);
	         long flag = service.updateByPrimaryKeySelective(mAfterSalesReasons);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[售后原因表]详情请求参数
      * @return MAfterSalesReasonsResponse 查询[售后原因表]详情响应参数
      * author 上海奇陌科技有限公司 
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[售后原因表]详情")
      @ResponseBody
     public RestResult<MAfterSalesReasonsResponse> getByPrimaryKey(@Validated  @RequestBody MAfterSalesReasonsRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	MAfterSalesReasons mAfterSalesReasons = new MAfterSalesReasons();
	     	BeanUtils.copyProperties(request, mAfterSalesReasons);
	     	MAfterSalesReasons result = service.getByPrimaryKey(mAfterSalesReasons);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     MAfterSalesReasonsResponse bo = new MAfterSalesReasonsResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new MAfterSalesReasonsResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<MAfterSalesReasons>> listPage (@RequestBody MAfterSalesReasonsRequest request) throws Throwable {
      MAfterSalesReasons param = new MAfterSalesReasons();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *M_AFTER_SALES_REASONS 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [售后原因表] ")
      @ResponseBody
      public RestResult<List<MAfterSalesReasonsResponse>>  listMAfterSalesReasonsAllRecord(@RequestBody MAfterSalesReasonsRequest request) throws Throwable {
      		MAfterSalesReasons param = new MAfterSalesReasons();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<MAfterSalesReasons> list = service.listAllRecord(param);
			List<MAfterSalesReasonsResponse> responseList = list.stream().map(temp -> {
			MAfterSalesReasonsResponse bo = new MAfterSalesReasonsResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }

     }

	
